﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleCmdProc;
using SplitCode;
using System.Threading;
using System.IO;

namespace TiffSplitConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            CommandLine cmdl = new CommandLine(true);
            cmdl.DefineOption("file", "Input file or directory", true, false);
            cmdl.DefineOption("j", "Jpeg compression quality, [0-10]", false, false);
            cmdl.DefineOption("outDir", "Output directory; if not specified same as input", false, false);
            cmdl.DefineFlag("ow", "Overwrite files if they exist", false);
            cmdl.DefineFlag("pdf", "Do PDF split instead of TIF split", false);
            cmdl.DefineFlag("del", "Delete processed inputs", false);
            if (!cmdl.LoadArguments(args))
                return;

            ConfObj conf = new ConfObj();
            conf.outputType = OutputType.TIF;
            conf.quality = -1;
            conf.inputFile = cmdl.GetOptionValue("file")[0];
            Console.WriteLine("Input file/directory: " + conf.inputFile);
            bool delInputs = false;

            if (cmdl.IsOptionSupplied("j"))
            {
                conf.quality = Convert.ToInt32(cmdl.GetOptionValue("j")[0]);
                conf.outputType = OutputType.JPG;
                Console.WriteLine("JPEG compression: " + conf.quality);
            }

            if (cmdl.IsOptionSupplied("outDir"))
            {
                conf.outDir = cmdl.GetOptionValue("outDir")[0];
            }

            if (cmdl.IsFlagSupplied("ow"))
            {
                conf.doOverwrite = true;
            }

            if (cmdl.IsFlagSupplied("pdf"))
            {
                conf.outputType = OutputType.PDF;
            }
           
            string [] inputFiles;
            if (Directory.Exists(conf.inputFile))   // it is a directory
            {
                if (conf.outputType == OutputType.PDF)
                    inputFiles = Directory.GetFiles(conf.inputFile, "*.pdf");
                else
                    inputFiles = Directory.GetFiles(conf.inputFile, "*.tif");
            }
            else
            {
                inputFiles = new string[] { conf.inputFile };
            }

            if (cmdl.IsFlagSupplied("del"))
                delInputs = true;

            UpdateProgress updateProgress = new UpdateProgress(ProgressChanged);
            int numOfPages = 0;

            foreach (string f in inputFiles)
            {
                conf.inputFile = f;
                RetObj retObj = TiffSplitCode.Prepare(conf, out numOfPages);
                conf.fromPage = 1;
                conf.toPage = numOfPages;
                RetObj retObj2 = TiffSplitCode.Split(conf, updateProgress);

                Console.WriteLine(retObj.ToString());

                if (delInputs)
                {
                    if (retObj.isOK)
                    {
                        File.Delete(f);
                    }
                }
            }

        }

        private static void ProgressChanged(int percentage)
        {
            Console.WriteLine(percentage + "%");
        }

        
    }
}
